﻿using KouDai.Core.Service.Navigation;
using KouDai.Core.ViewModel;
using KouDai.Metro.Views;
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace KouDai.Metro.Service
{
    public class MetroNavigateService : SimpleNavigateService<Type>
    {
        public MetroNavigateService()
        {
            Frame.Navigated += OnNavigated;
            Register(typeof(MainViewModel), typeof(MainPage));
            Register(typeof(ItemDetailViewModel), typeof(ItemDetailPage));
        }

        public Frame Frame
        {
            get
            {
                return (Frame)Window.Current.Content;
            }
        }

        void OnNavigated(object sender, NavigationEventArgs e)
        {
            object parameter = e.Parameter;
            Page metroPage = Frame.Content as Page;
            NavigateViewModel dataContext = metroPage.DataContext as NavigateViewModel;
            if (dataContext != null)
            {
                dataContext.Data = parameter;
            }
        }

        public override bool CanGoBack
        {
            get { return this.Frame.CanGoBack; }
        }

        protected override void NavigateTo(Type view,object parameter)
        {
            Frame.Navigate(view, parameter);
        }

        protected override void GoBackCore()
        {
            Frame.GoBack();
        }
    }
}
